<!--
 * @Description: 设施类型管理页面
-->
<template>
  <BaseMainSlot>
    <!-- <template v-slot:btn-box>
      <ActionBar
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :edit-active="editActive"
        :add-active="addActive"
        :del-active="delActive"
        :has-edit="false"
        :has-cancel="false"
        :has-save="false"
        @add="add"
        @cancel="cancel('formData')"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template> -->
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">设施类型名称:</div>
          <div class="input">
            <el-input v-model="facilityTypeName" placeholder="请输入设施类型名称" clearable />
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <el-table
        ref="tableData"
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        class="tableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="50" />
        <!-- <el-table-column prop="facilityTypeName" label="设施类型名称" /> -->
        <el-table-column prop="facilityCategory" label="设施类别" />
        <!-- <el-table-column width="120" label="操作">
          <template slot-scope="scope" v-if="scope.row.facilityTypeId">
            <el-button class="text-btn-cursor" type="text" size="small" @click="handleEdit(scope.row)"><i class="el-icon-edit-outline"></i> 修改</el-button>
            <el-button class="text-btn-cursor cursor-delete" type="text" size="small" @click="handleDelete(scope.row)"><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog :title="dialogTitle + '投入成本信息'" :visible.sync="dialogFormVisible" :before-close="cancel">
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <!-- <el-form-item label="设施类型名称:" prop="facilityTypeName" class="w100p">
            <el-input v-model="formData.facilityTypeName" placeholder="请输入设施类型名称"  clearable />
          </el-form-item> -->
          <el-form-item label="设施类别:" prop="facilityCategory" class="w100p">
            <!-- <el-input v-model="formData.test" placeholder="请输入岗位名称"  clearable /> -->
            <el-select v-model="formData.facilityCategory" filterable clearable>
              <el-option label="种养设施" value="种养设施" />
              <el-option label="加工设施" value="加工设施" />
              <el-option label="办公设施" value="办公设施" />
              <el-option label="其他" value="其他" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="备注:" prop="facilityTypeDes" class="w100p">
            <el-input
              v-model="formData.facilityTypeDes"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"

              clearable
            />
          </el-form-item> -->
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button class="zh-button-two" type="primary" @click="save('formData')">
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
// import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import facilityTypeManage from "@/api/basic-info-manage/facility-type-manage";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import SearchBox from "@/components/SearchBox";
import { mymixin } from "@/utils/mixin";
export default {
  name: "",
  components: {
    // ActionBar,
    Pagination,
    BaseMainSlot, SearchBox },
  mixins: [mymixin],
  data() {
    return {
      userName: "",
      searchList: {
        startTime: "",
        endTime: ""
      },
      ipmentType: [],
      facilityTypeName: '',
      rules: {
        facilityTypeName: [{ required: true, message: '请输入设施类型名称', trigger: 'blur' }],
        facilityCategory: [{ required: true, message: '请选择设施类别', trigger: 'change' }]
      },
      dialogTitle: "",
      dialogFormVisible: false
    };
  },
  created() {
    //  表格数据
  },
  mounted() {
    // this.getipmentTypeList();
    this.getTableDataList();
  },
  methods: {
    // 获取表格数据
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          facilityTypeName: this.facilityTypeName
        }
      }
      facilityTypeManage.getfacilityTypeList(params).then(res => {
        this.loading = false;
        let data = res.data;
        if (data) {
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // getipmentTypeList() {
    //   facilityTypeManage.getipmentTypeList().then(res => {
    //     let data = res.data;
    //     this.ipmentType = data;
    //     // console.log(this.ipmentType);
    //   });
    // },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    add() {
      this.formData = {};
      this.openDialog('add');
    },
    handleEdit(row) {
      // console.log(row);
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify(row));
      // this.addActive = true;
      // // this.addActive = false;
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
      this.canInput = false;
      this.$refs.formData.resetFields();
      this.openDialog('edit');
    },
    handleSelectionChange(list) {
      this.facilityTypeIdList = [];
      this.facilityTypeIdList = list.map(item => item.facilityTypeNo);
      if (this.facilityTypeIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    selectRow(val) {
      this.selectlistRow = val;
    },
    delmessage(formData) {
      facilityTypeManage.delfacilityTypeInfo(this.facilityTypeIdList).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.$refs[formData].resetFields();
          this.getTableDataList();
        } else {
          this.$message.warning(res.msg || '删除失败');
        }
      });
    },
    handleDelete(data) {
      facilityTypeManage.delfacilityTypeInfo([data.facilityTypeNo]).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          // this.$refs[formData].resetFields();
          this.getTableDataList();
        } else {
          this.$message.warning(res.msg || '删除失败');
        }
      });
    },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            // 巡检点数组赋值进formData
            // console.log(this.formData);
            facilityTypeManage.editfacilityTypeInfo(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                this.closeDialog();
                this.$refs[formData].resetFields();
                this.getTableDataList();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            // 新增接口
            // this.formData.organizationNo = sessionStorage.organizationNo
            facilityTypeManage.addfacilityTypeInfo(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                this.closeDialog();
                this.$refs[formData].resetFields();
                this.getTableDataList();
              } else {
                this.$message.error(res.msg || "新增失败！");
              }
            });
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === 'add' ? '新增' : '编辑'
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = '';
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";
.buttons {
  background-color: rgba(255, 255, 255, 0);
  border-color: $inputBorderColor;
  color: $appMainTextColor;
}
.buttonBox{
  margin-bottom: 10px;
  display: flex;
  justify-content: space-around;
}

  ::v-deep .el-form-item__label {
    min-width: 120px !important;
    text-align: end;
  }
  .el-form {
  /*.*/
  .el-table {
    width: 200px !important;
    // .el-table__header{
    //   width: 200px;
    // }
  }
}
</style>
